#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define eb emplace_back
#define ep emplace
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;
using db = double;
using tl = tuple < int, int, int >;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 2e5 + 10;

ll tot;
int n, m;
veg pot[N];
vec tpot[N];

struct DSU {
	int fa[N];
	void reset() { rep(i, 0, n + 1) fa[i] = i; }
	int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); }
} t[2];

struct BIT {
	ll tr[N];
	void add(int x, ll v) { for(; x <= n; x += x & -x) tr[x] += v; }
	ll query(int x) { ll res = 0; for(; x; x -= x & -x) res += tr[x]; return res; }
} T;
	
int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	n = in; rep(i, 1, n) tpot[in].eb(i);
	m = in; t[0].reset(); t[1].reset();
	rep(i, 1, m) {
		int x = in, y = in, c = in;
		pot[y].eb(x, c);
	}
	rep(i, 1, n) {
		for(auto v : pot[i]) {
			ll res = T.query(v.fi);
			if(res < v.se) tot += res, T.add(t[0].gf(v.fi) + 1, v.se - res), T.add(t[1].gf(v.fi), res - v.se);
			else tot += v.se;
		} for(auto v : tpot[i]) t[0].fa[v] = v - 1, t[1].fa[v] = v + 1;
	}
	printf("%lld\n", tot);
	return 0;
}
